$(function() {
    update_dep.initial();
});

var update_dep = {

    initial : function initial() {
        this.bindSubmitForm();
    },

    bindSubmitForm : function bindSubmitForm() {
        var form = $("form#dep-update-form");

        $("#update-dep-btn").click(function() {
            var result = update_dep.verifyInput();
            if (result) {
                var data = new Object();
                data.depName = $("#depname").val();
                data.memo = $("#depmemo").val();
                data.depId = $("#depId").val();
                $.ajax({
                    headers : {
                        'Accept' : 'application/json',
                        'Content-Type' : 'application/json'
                    },
                    type : "POST",
                    url : form.attr("action"),
                    data : JSON.stringify(data),
                    success : function(message, tst, jqXHR) {
                        if (message.result == "success") {
                            util.success("修改部门成功！")
                            document.location.href = document
                                    .getElementsByTagName('base')[0].href
                                + util.getCurrentRole() + "/common/dep-list";
                        } else {
                            util.error(message.result);
                        }
                    }
                });
            }

            return false;
        });
    },

    verifyInput : function verifyInput() {
        $(".form-message update-message").empty();
        var result = true;
        var check_n = this.checkName();
        // var check_e = this.checkEmail();
        var check_m = this.checkMemo();

        result = check_n && check_m;
        return result;
    },

    checkName : function checkName() {
        var f_name = $(".form-dep-name-update input").val();
        if (f_name == "") {
            $(".form-dep-name-update .form-message").text("题库名不能为空");
            return false;
        } else if (f_name.length > 40 || f_name.length < 3) {
            $(".form-dep-name-update .form-message").text("请保持在3-40个字符以内");
            return false;
        }
        $(".form-dep-name-update .form-message").text("");
        return true;
    },

    checkMemo : function checkMemo() {
        var memo = $(".form-dep-desc-update input").val();
        if (memo == "") {
            $(".form-dep-desc-update .form-message").text("描述不能为空");
            return false;
        } else if (memo.length > 40 || memo.length < 3) {
            $(".form-dep-desc-update .form-message").text("请保持在3-40个字符以内");
            return false;
        }
        $(".form-dep-desc-update .form-message").text("");
        return true;
    }

};

function validUpdateParam(){
    update_dep.verifyInput();
}